package cn.com.member.controller.mine;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_enums;
import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiTmember;
import cn.com.api.entity.ApiU_match;
import cn.com.api.entity.ApiU_match_prize;
import cn.com.api.service.ApiT_enumsService;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiU_matchService;
import cn.com.api.service.ApiU_match_prizeService;
import cn.com.util.JsonUtil;
import cn.com.util.Utils;

@Controller
public class MatchController {

	@Autowired
	private ApiU_matchService apiU_matchService;
	@Autowired
	private ApiT_memberService apiT_memberService;
	@Autowired
	private ApiT_enumsService apiT_enumsService;
	@Autowired
	private ApiU_match_prizeService apiU_match_prizeService;

	private static Logger logger = Logger.getLogger(MatchController.class);

	/**
	 * 获取我的赛事列表
	 */
	@RequestMapping(value = "match-game")
	public ModelAndView match_list(HttpServletRequest request, HttpServletResponse response) {

		ModelAndView mv = new ModelAndView("/member/MyMatch");
		try {

			ApiTmember member = (ApiTmember) request.getSession().getAttribute("webuser");

			String user_id = member.getCid().toString();

			String match_cate = request.getParameter("match_cate");

			Map<String, Object> param = new HashMap<String, Object>();

			param.put("user_id", user_id);

			param.put("match_cate", match_cate);
			mv.addObject("match_cate", match_cate);

			logger.info("======[MatchController[match_list[参数user_id:" + user_id + ",match_cate:" + match_cate
					+ "]]]=======");

			if (match_cate != null && "3".equals(match_cate)) {

				List<ApiU_match> u_match = apiU_matchService.findU_matchs_award(param);
				mv.addObject("u_matchs", u_match);

			}

			else if (match_cate != null && "2".equals(match_cate)) {

				List<ApiU_match> match = apiU_matchService.findU_matchs_vote(param);
				mv.addObject("u_matchs", match);

			} else {
				List<ApiU_match> u_matchs = apiU_matchService.findU_matchs_main(param);
				mv.addObject("u_matchs", u_matchs);

			}

		} catch (Exception e) {
			logger.error("===========[matchController[match_list[获取赛事列表失败]]]========", e);
		}
		return mv;
	}

	/**
	 * 详情页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "match-detail-info")
	public ModelAndView match_detail_info_pre(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("/member/MyMatch_detail");
		try {
			List<ApiT_enums> cate = apiT_enumsService.findT_enumssByItem_key("match_cates_list");
			List<ApiT_enums> cate_list = apiT_enumsService.findT_enumssByParent_id(cate.get(0).getId() + "");
			mv.addObject("match_cates_list", cate_list);
			if (request.getParameter("id") != null) {
				String id = request.getParameter("id");
				ApiU_match u_match = apiU_matchService.findU_matchById(Integer.valueOf(id));
				List<ApiU_match_prize> list_match_prize = apiU_match_prizeService.findU_match_prizesByMatchId(id);

				mv.addObject("u_match", u_match);
				mv.addObject("list_match_prize", list_match_prize);
			}
		} catch (Exception e) {
			logger.error("===========[matchController[match_detail_info[获取详情信息失败]]]========", e);
		}
		return mv;
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 */

	@RequestMapping(value = "match-delete")
	public void u_match_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiU_matchService.deleteU_match(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
